package com.tgu.home_action.service.impl;

import com.tgu.home_action.dao.AdminMapper;
import com.tgu.home_action.dao.NoticeMapper;
import com.tgu.home_action.pojo.*;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.NoticeService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private AdminMapper adminMapper;

    /**
     * 获取公告
     */
    @Override
    public ResponseData<List<Notice>> getNotices(Notice notice) {
        List<Notice> notices = noticeMapper.getNotices(notice);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), notices);
    }

    /**
     * 获取Banner
     */
    @Override
    public ResponseData<List<Banner>> getBanners(Banner banner) {
        List<Banner> banners = noticeMapper.getBanners(banner);
        System.out.println(banners);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), banners);
    }

    /**
     * 管理员查询公告
     */
    @Override
    public ResponseData<Page<Notice>> adminGetNotices(Notice notice) {
        int sum = noticeMapper.getNoticesSum(notice);
        if (notice.getPageNo() == null || notice.getPageSize() == null) {
            notice.setPageNo(1);
            notice.setPageSize(sum);
        }
        notice.setStartSize((notice.getPageNo() - 1) * notice.getPageSize());
        List<Notice> notices = noticeMapper.adminGetNotice(notice);
        int pageNum;
        if (notice.getPageSize() == 0) {
            pageNum = 0;
        } else {
            pageNum = sum / notice.getPageSize();
            if (sum % notice.getPageSize() > 0) {
                pageNum++;
            }
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), new Page<>(notice.getPageNo(), notice.getPageSize(), pageNum, sum, notices));

    }

    /**
     * 发布公告
     */
    @Override
    public ResponseData<Object> postNotice(Notice notice, Admin adminUser) {
        notice.setPublisherId(adminUser.getId());
        notice.setTime(new Timestamp(new Date().getTime()));
        adminUser = adminMapper.getById(adminUser.getId());
        notice.setPublisher(adminUser.getName());
        noticeMapper.insertNotice(notice);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 修改公告
     */
    @Override
    public ResponseData<Object> updateNotice(Notice notice) {
        Boolean isSuccess = noticeMapper.updateNotice(notice);
        return isSuccess ? new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue())
                : new ResponseData<>(ResponseState.NOTICE_NOT_EXIST.getMessage(), ResponseState.NOTICE_NOT_EXIST.getValue());
    }

    /**
     * 删除公告
     */
    @Override
    public ResponseData<Object> delNotice(Integer id) {
        noticeMapper.delBannerByNoticeId(id);
        Boolean isSuccess = noticeMapper.delNotice(id);
        return isSuccess ? new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue())
                : new ResponseData<>(ResponseState.NOTICE_NOT_EXIST.getMessage(), ResponseState.NOTICE_NOT_EXIST.getValue());

    }

    /**
     * 通过ID查询公告
     */
    @Override
    public ResponseData<Notice> getNoticeById(Integer id) {
        Notice notice = noticeMapper.getNoticeById(id);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), notice);
    }

    /**
     * 管理员查询Banner
     */
    @Override
    public ResponseData<Object> adminGetBanner(Notice notice) {
        int sum = noticeMapper.getBannersSum(notice);
        if (notice.getPageNo() == null || notice.getPageSize() == null) {
            notice.setPageNo(1);
            notice.setPageSize(sum);
        }
        notice.setStartSize((notice.getPageNo() - 1) * notice.getPageSize());
        List<Banner> banners = noticeMapper.adminGetBanner(notice);
        int pageNum;
        if (notice.getPageSize() == 0) {
            pageNum = 0;
        } else {
            pageNum = sum / notice.getPageSize();
            if (sum % notice.getPageSize() > 0) {
                pageNum++;
            }
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), new Page<>(notice.getPageNo(), notice.getPageSize(), pageNum, sum, banners));

    }

    /**
     * 发布Banner
     */
    @Override
    public ResponseData<Object> postBanner(Banner banner, Admin adminUser) {
        System.out.println("banner.getNoticeId"+banner.getNoticeId());
        Notice notice = noticeMapper.getNoticeById(banner.getNoticeId());

        if (notice == null) {
            return new ResponseData<>(ResponseState.NOTICE_NOT_EXIST.getMessage(), ResponseState.NOTICE_NOT_EXIST.getValue());
        }
        banner.setTime(new Timestamp(new Date().getTime()));
        noticeMapper.insertBanner(banner);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 修改Banner
     */
    @Override
    public ResponseData<Object> updateBanner(Banner banner) {
        if (banner.getNoticeId() != null) {
            Notice notice = noticeMapper.getNoticeById(banner.getNoticeId());
            if (notice == null) {
                return new ResponseData<>(ResponseState.NOTICE_NOT_EXIST.getMessage(), ResponseState.NOTICE_NOT_EXIST.getValue());
            }
        }
        Boolean isSuccess = noticeMapper.updateBanner(banner);
        return isSuccess ? new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue())
                : new ResponseData<>(ResponseState.BANNER_NOT_EXIST.getMessage(), ResponseState.BANNER_NOT_EXIST.getValue());
    }

    /**
     * 删除Banner
     */
    @Override
    public ResponseData<Object> delBanner(Integer id) {
        Boolean isSuccess = noticeMapper.delBanner(id);
        return isSuccess ? new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue())
                : new ResponseData<>(ResponseState.BANNER_NOT_EXIST.getMessage(), ResponseState.BANNER_NOT_EXIST.getValue());

    }

    /**
     * 通过ID查询Banner
     */
    @Override
    public ResponseData<Banner> getBannerById(Integer id) {
        Banner banner = noticeMapper.getBannerById(id);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), banner);
    }

}
